﻿//-------------------------------------------------------------------------------------------
// <copyright file="TrustLineConverter.cs" company="Russell Wilkins">
//     Copyright (c) Russell Wilkins 2014. All Rights Reserved.
// </copyright>
// <author>Russell Wilkins</author>
// <license href="license.txt">
//     The use of this software is governed by the Microsoft Public License
//     which is included with this distribution.
// </license>
//-------------------------------------------------------------------------------------------
namespace Ripple.Data.Converters
{
    using Helpers;
    using Providers;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    /// <summary>
    /// Class to convert between line dto and entity.
    /// </summary>
    internal static class TrustLineConverter
    {
        internal static IEnumerable<TrustLine> ToEntities(this IEnumerable<AccountLineData> list, IRippleContext rippleContext, string accountId)
        {
            foreach (var item in list)
            {
                yield return item.ToEntity(rippleContext, accountId);
            }
        }

        internal static TrustLine ToEntity(this AccountLineData data, IRippleContext rippleContext, string accountId)
        {
            if (data == null)
            {
                return null;
            }

            TrustLine trustLine = new TrustLine(rippleContext);
            trustLine.AccountId = accountId;
            trustLine.UpdateEntity(data);
            return trustLine;
        }

        internal static void UpdateEntity(this TrustLine entity, AccountLineData data)
        {
            if (data == null)
            {
                return;
            }

            entity.PeerAccountId = data.account;
            entity.Balance = data.balance.ToDouble();
            entity.Currency = data.currency;
            entity.Limit = data.limit.ToDouble();
            entity.PeerLimit = data.limit_peer.ToDouble();
            entity.QualityIn = data.quality_in;
            entity.QualityOut = data.quality_out;
            entity.NoRipple = data.no_ripple;
            entity.NoRipplePeer = data.no_ripple_peer;
            entity.Updated = DateTime.Now;
        }
    }
}